package com.ch.forum.service.impl;

import com.ch.forum.bo.ResponseResult;
import com.ch.forum.dao.BuySaleRewardMapper;
import com.ch.forum.dao.UserMapper;
import com.ch.forum.model.BuySaleReward;
import com.ch.forum.model.UserModel;
import com.ch.forum.service.BuySaleRewardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

@Service
public class BuySaleRewardServiceImpl implements BuySaleRewardService{

    @Autowired
    private BuySaleRewardMapper buySaleRewardMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public ResponseResult recharge(BuySaleReward param) {
        if(param == null){
            return new ResponseResult(false, "参数不能为空");
        }
        UserModel userModel = userMapper.findById(param.getUserId());
        if(userModel == null){
            return new ResponseResult(false, "找不到用户");
        }
        param.setDate(new Date(System.currentTimeMillis()));
        param.setType(1);
        param.setStatus(0);
        buySaleRewardMapper.recharge(param);
        return new ResponseResult(true, "操作成功");
    }

    @Override
    public ResponseResult reflect(BuySaleReward param) {
        if(param == null){
            return new ResponseResult(false, "参数不能为空");
        }
        UserModel userModel = userMapper.findById(param.getUserId());
        if(userModel == null){
            return new ResponseResult(false, "找不到用户");
        }
        param.setDate(new Date(System.currentTimeMillis()));
        param.setType(2);
        param.setStatus(0);
        buySaleRewardMapper.reflect(param);
        return new ResponseResult(true, "操作成功");
    }

    @Override
    public ResponseResult buyIn(BuySaleReward param) {
        if(param == null){
            return new ResponseResult(false, "参数不能为空");
        }
        UserModel userModel = userMapper.findById(param.getUserId());
        if(userModel == null){
            return new ResponseResult(false, "找不到用户");
        }
        if(userModel.getVirtualPoint() == null){
            return new ResponseResult(false, "账户没有虚拟积分");
        }
        Double vPoint = userModel.getVirtualPoint();
        Double ponit = param.getPrice() * param.getNum();
        if(ponit.compareTo(vPoint) > 0){
            return new ResponseResult(false, "積分不夠買入");
        }
        param.setDate(new Date(System.currentTimeMillis()));
        param.setType(3);
        param.setStatus(1);
        buySaleRewardMapper.buyIn(param);
        userMapper.updateVirtualPoint(param.getUserId(), userModel.getVirtualPoint() - ponit);
        return new ResponseResult(true, "买入成功");
    }

    @Override
    public ResponseResult saleOut(BuySaleReward param) {
        if(param == null){
            return new ResponseResult(false, "参数不能为空");
        }
        UserModel userModel = userMapper.findById(param.getUserId());
        if(userModel == null){
            return new ResponseResult(false, "找不到用户");
        }
        if(userModel.getVirtualPoint() == null){
            return new ResponseResult(false, "账户没有虚拟积分");
        }
        Double vPoint = userModel.getVirtualPoint();
        Double ponit = param.getPrice() * param.getNum();
        if(ponit.compareTo(vPoint) > 0){
            return new ResponseResult(false, "積分不夠賣出");
        }
        param.setDate(new Date(System.currentTimeMillis()));
        param.setType(4);
        param.setStatus(1);
        buySaleRewardMapper.buyIn(param);
        userMapper.updateVirtualPoint(param.getUserId(), userModel.getVirtualPoint() - ponit);
        return new ResponseResult(true, "卖出成功");
    }

    @Override
    public ResponseResult updatePoint(Long userId, int type, Long rewardId){
        UserModel userModel = userMapper.findById(userId);
        if(userModel == null){
            return new ResponseResult(false, "找不到用户");
        }
        Map voMap = buySaleRewardMapper.findById(rewardId);
        if(voMap == null){
            return new ResponseResult(false, "找不到交易记录");
        }
        Double point = (Double) voMap.get("price") * (int)voMap.get("num");
        if(type == 1){
            userMapper.updatePoint((Long) voMap.get("userId"), userModel.getPoint() + point);
        }
        if(type == 2){
            userMapper.updatePoint((Long) voMap.get("userId"), userModel.getPoint() - point);
        }
        return new ResponseResult(true, "操作成功");
    }

    @Override
    public ResponseResult rechargeList() {
        return new ResponseResult(true, "数据", buySaleRewardMapper.findRechargeList());
    }

    @Override
    public ResponseResult findReflectList() {
        return new ResponseResult(true, "数据", buySaleRewardMapper.findReflectList());
    }

    @Override
    public ResponseResult updateStatus(Long id) {
        Map findMap = buySaleRewardMapper.findById(id);
        if(findMap == null){
            return new ResponseResult(false, "记录不存在，请先核对");
        }
        buySaleRewardMapper.updateStatus(id);
        return new ResponseResult(true, "修改成功");
    }

    @Override
    public ResponseResult findUserReard(Long id) {
        if(id == null){
            return new ResponseResult(false, "参数不能为空");
        }
        return new ResponseResult(true, "数据", buySaleRewardMapper.findUserReard(id));
    }
}
